package com.cxyd.modules.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.constant.Constant;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.api.dao.RmsNoticeDao;
import com.cxyd.modules.api.dto.RmsNoticeDTO;
import com.cxyd.modules.api.entity.RmsNoticeEntity;
import com.cxyd.modules.api.service.RmsNoticeService;
import com.cxyd.modules.wms.entity.LogWorkEntity;
import com.cxyd.modules.wms.service.LogWorkService;
import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
@Slf4j
public class RmsNoticeServiceImpl extends CrudServiceImpl<RmsNoticeDao, RmsNoticeEntity, RmsNoticeDTO> implements RmsNoticeService {
    @Resource
    private LogWorkService logWorkService;

    @Override
    public void noticeProcess(RmsNoticeDTO dto) {
        try {
            //日志记录
            LogWorkEntity insertLogWork = new LogWorkEntity();
            insertLogWork.setWorkName("称重信息记录");
//            insertLogWork.setRelateNo(dto.getContNo());
            insertLogWork.setWorkMsg(dto.toString());
            insertLogWork.setWorkProc("RmsNoticeServiceImpl.noticeProcess");
            insertLogWork.setCreator(Constant.ClientCode.WMS.getId());
            insertLogWork.setCreateDate(new Date());
            logWorkService.logWorkInsert(insertLogWork);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("日志记录失败！" + e.getMessage());
        }
//        判断所给信息是否已经给过,如果给过则更新即可（无需再次写入）
        QueryWrapper<RmsNoticeEntity> qw = new QueryWrapper<>();
        qw.eq("cont_no", dto.getContNo()).last("LIMIT 1");
        RmsNoticeEntity existEntity = baseDao.selectOne(qw);
        if (existEntity == null) {
            RmsNoticeEntity entity = ConvertUtils.sourceToTarget(dto, RmsNoticeEntity.class);
            baseDao.insert(entity);
            return;
        }
        existEntity.setContHeight(dto.getContHeight());
        existEntity.setContWeight(dto.getContWeight());
        existEntity.setReqTime(dto.getReqTime());
        baseDao.updateById(existEntity);
    }

    @Override
    public QueryWrapper<RmsNoticeEntity> getWrapper(Map<String, Object> params) {
        return null;
    }
}
